#-*-coding:utf8-*-
from django.http import HttpResponse
from Model.models import Shop
import json
def index(request):
    '''
    返回店铺首页数据
    限制20条
    代码很丑
    暂时先这么着
    @author cxy
    @email caixiangyue@outlook.com
    @date 2017-9-19 9:49
    '''
    shops = Shop.objects.all()[:20:-1]
    shopList = []
    pics = []
    for shop in shops:
        shopId = shop.id
        shopAvatar = shop.avatar
        shopName = shop.shop_name
        shopAddress = shop.detail_address
        price = shop.price
        shopPrice = shop.original_price
        html=shop.html
        shopList.append({'shopId':shopId,'shopAvatar':shopAvatar,'shopName':shopName,
        'shopAddress':shopAddress,'price':price,
        'shopPrice':shopPrice,'html':html,
        })
        pics.append(shop.shop_pic)
    
    if request.method == 'GET':
        data = {'pics':pics,'shopList':shopList}
        return HttpResponse(json.dumps(data, ensure_ascii=False), content_type="application/json;charset=utf8")
    else:
        return HttpResponse(json.dumps({'error':404}, ensure_ascii=False), content_type="application/json;charset=utf8")
